<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="70px"
             :tree-props="{children: 'children', hasChildren: 'hasChildren'}">
      <el-form-item label="工单编码" prop="workorderCode">
        <el-input
          v-model="queryParams.workorderCode"
          placeholder="请输入工单编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="工单名称" prop="workorderName">
        <el-input
          v-model="queryParams.workorderName"
          placeholder="请输入工单名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="来源单据" prop="sourceCode">
        <el-input
          v-model="queryParams.sourceCode"
          placeholder="请输入来源单据"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="产品编号" prop="productCode">
        <el-input
          v-model="queryParams.productCode"
          placeholder="请输入产品编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="产品名称" prop="productName">
        <el-input
          v-model="queryParams.productName"
          placeholder="请输入产品名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="客户编码" prop="clientCode">
        <el-input
          v-model="queryParams.clientCode"
          placeholder="请输入客户编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="客户名称" prop="clientName">
        <el-input
          v-model="queryParams.clientName"
          placeholder="请输入客户名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="需求日期" prop="requestDate">
        <el-date-picker
          style="width: 208px"
          v-model="queryParams.requestDate"
          type="date"
          placeholder="请选择需求日期"
          value-format="yyyy-MM-dd"
          format="yyyy-MM-dd"
          clearable
          @change="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd()"
          v-hasPermi="['system:post:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:post:export']"
        >导出
        </el-button>
      </el-col>
    </el-row>

    <el-table :data="postList" style="width: 100%;margin-bottom: 20px;"
              row-key="workorderId"
              default-expand-all
              :tree-props="{children: 'children', hasChildren: 'hasChildren'}">
      <el-table-column label="工单编码" align="left" prop="workorderCode" width="200" header-align="center"/>
      <el-table-column label="工单名称" align="center" prop="workorderName" width="200"/>
      <el-table-column label="工单来源" align="center" prop="orderSource">
        <template slot="default" slot-scope="scope">
          <dict-tag :options="dict.type.sys_workorder" :value="scope.row.orderSource"/>
        </template>
      </el-table-column>
      <el-table-column label="订单编号" align="center" prop="sourceCode" width="150"/>
      <el-table-column label="产品编号" align="center" prop="productCode" width="150"/>
      <el-table-column label="产品名称" align="center" prop="productName" width="120"/>
      <el-table-column label="规格型号" align="center" prop="productSpc" width="100"/>
      <el-table-column label="单位" align="center" prop="unitOfMeasure" width="100"/>
      <el-table-column label="工单数量" align="center" prop="quantity" width="100"/>
      <el-table-column label="调整数量" align="center" prop="quantityChanged" width="100"/>
      <el-table-column label="已生产数量" align="center" prop="quantityProduced" width="100"/>
      <el-table-column label="客户编码" align="center" prop="clientCode" width="100"/>
      <el-table-column label="客户名称" align="center" prop="clientName" width="100"/>
      <el-table-column label="需求日期" align="center" prop="requestDate" width="150"/>
      <el-table-column label="单据状态" align="center" prop="status" width="130"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="180"  fixed="right">
        <template slot-scope="scope">
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-edit"-->
<!--            @click="handleUpdate(scope.row)"-->
<!--            v-hasPermi="['system:post:edit']"-->
<!--          >修改-->
<!--          </el-button>-->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-plus"
            @click="handleAdd(scope.row)"
            v-hasPermi="['system:post:edit']"
          >添加
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:post:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改岗位对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="110px">
        <!--        <el-form-item label="父工单ID" prop="parendId">-->
        <!--          <el-input v-model="form.parentId" placeholder="请输入工艺路线编号" :disabled="dis"/>-->
        <!--        </el-form-item>-->
        <el-row>
          <el-col :span="12">
            <el-form-item label="工单编码" prop="workorderCode">
              <el-input v-model="form.workorderCode" placeholder="请输入工单编码"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="工单名称" prop="workorderName">
              <el-input v-model="form.workorderName" placeholder="请输入工单名称"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="来源类型" prop="orderSource">
              <el-radio-group v-model="form.orderSource" @change="handleOrderSourceChange" >
                <el-radio
                  v-for="dict in dict.type.sys_workorder"
                  :key="dict.value"
                  :label="dict.value"
                >{{dict.label}}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="订单编号" prop="sourceCode" v-if="orderSourceable">
              <el-input v-model="form.sourceCode" placeholder="请输入订单编号"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="单据状态" prop="status">
              <el-input v-model="form.status" :disabled="true"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="产品编号" prop="productCode">
              <el-input placeholder="请选择产品" v-model="form.productCode" class="input-with-select"
                        :disabled="true">
                <el-button slot="append" icon="el-icon-search" @click="search"></el-button>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="产品名称" prop="productName">
              <el-input v-model="form.productName" placeholder="请选择产品" :disabled="true"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="规格型号" prop="productSpc">
              <el-input v-model="form.productSpc" placeholder="请选择产品" :disabled="true"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="单位" prop="unitOfMeasure">
              <el-input v-model="form.unitOfMeasure" placeholder="请选择产品" :disabled="true"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="工单数量" prop="quantity">
              <el-input-number v-model="form.quantity" @change="handleChange" :min="1"
                               label="描述文字"></el-input-number>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="需求日期" prop="requestDate">
              <el-input v-model="form.requestDate" placeholder="请输入单位"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="客户编码" prop="clientCode" v-if="orderSourceable">
              <el-input placeholder="请输入客户编码" v-model="form.clientCode" class="input-with-select"
                        :disabled="true">
                <el-button slot="append" icon="el-icon-search" @click="search2"></el-button>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="客户名称" prop="clientName" v-if="orderSourceable">
              <el-input v-model="form.clientName" placeholder="请输入客户名称" :disabled="true"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容"/>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <template>
      <div id="chart" style="width: 600px; height: 400px;"></div>

    </template>
    <mydialog v-if="mydialog" @dialog-closed="closemydialog" @submit="getInfo"></mydialog>
    <mydialog2 v-if="mydialog2" @dialog-closed2="closemydialog2" @submit2="getInfo2"></mydialog2>
  </div>
</template>

<script>
import {
  listWorkorder,
  getProcess,
  delWorkorder,
  addWorkorder,
  updateProcess,
  treelistWorkorder,
  logTotal
} from "../../../api/system/workorder"
import * as echarts from 'echarts'
import mydialog from "../process/dialog.vue";
import Mydialog2 from "../workorder/dialog2.vue"
import dict from "../dict/index.vue";

export default {
  name: "Workorder",
  computed: {
    dict() {
      return dict
    }
  },
  components: {Mydialog2, mydialog},
  dicts: ['sys_production_type', 'sys_workorder'],
  data() {
    return {
      mydialog: false,
      mydialog2: false,
      dis: false,
      orderSourceable: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 岗位表格数据
      postList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        workorderCode: undefined,
        workorderName: undefined,
        sourceCode: undefined,
        productCode: undefined,
        productName: undefined,
        clientCode: undefined,
        clientName: undefined,
        requestDate: undefined
      },
      // 表单参数
      form: {
        workorderCode: undefined,
        workorderName: undefined,
        orderSource: undefined,
        productSpc: undefined,
        unitOfMeasure: undefined,
        quantity: 1,
        itemCode: undefined,
        requestDate: undefined,
        sourceCode: undefined,
        productCode: undefined,
        productName: undefined,
        clientCode: undefined,
        clientName: undefined,
        workorderDesc: undefined,
        status: "草稿",
        remark: undefined,
        parentId: undefined,
      },
      // 表单校验
      rules: {
        workorderCode: [
          {required: true, message: "工单编码不能为空", trigger:["blur", "change"]}
        ],
        workorderName: [
          {required: true, message: "工单名称不能为空", trigger: ["blur", "change"]}
        ],
        orderSource: [
          {required: true, message: "工单来源不能为空", trigger: ["blur", "change"]}
        ],
        productCode: [
          {required: true, message: "产品编号不能为空", trigger: ["blur", "change"]}
        ],
        productName: [
          {required: true, message: "产品名称不能为空", trigger: ["blur", "change"]}
        ],
        unitOfMeasure: [
          {required: true, message: "单位不能为空", trigger: ["blur", "change"]}
        ],
        quantity: [
          {required: true, message: "工单数量不能为空", trigger: ["blur", "change"]}
        ]
      }
    }
  },
  created() {
    this.gettreeList()
  },
  mounted() {
    this.initChart()
  },
  methods: {

    //字典改变触发
    handleOrderSourceChange(){
      if(this.form.orderSource == "ORDER"){
        this.orderSourceable = true
      }else{
        this.orderSourceable = false
      }
    },
    getInfo(row) {
      this.form.productCode = row.itemCode
      this.form.productName = row.itemName
      this.form.unitOfMeasure = row.unitOfMeasure
      this.form.productSpc = row.specification
    },
    getInfo2(row) {
      this.form.clientName = row.clientName
      this.form.clientCode = row.clientCode
    },
    search() {
      this.mydialog = true
    },
    search2() {
      this.mydialog2 = true
    },
    closemydialog() {
      this.mydialog = false
    },
    closemydialog2() {
      this.mydialog2 = false
    },
    initChart() {
      const chart = echarts.init(document.getElementById('chart'))
      logTotal().then(res => {
          chart.setOption({
            title: {
              text: '日志'
            },
            tooltip: {
              trigger: 'axis'
            },
            legend: {
              data: ['数量']
            },
            xAxis: {
              type: 'category',
              data: res.data.categories
            },
            yAxis: {
              type: 'value'
            },
            series: [
              {
                name: '数量',
                type: 'bar',
                data: res.data.values
              }
            ]
          })
        }
      )
    },
    gettreeList() {
      treelistWorkorder().then(response => {
        console.log(response)
        this.postList = response.data
      })
    },
    /** 查询岗位列表 */
    getList() {
      this.loading = true
      listWorkorder(this.queryParams).then(response => {
        this.postList = response.data
        //this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        workorderCode: undefined,
        workorderName: undefined,
        orderSource: undefined,
        productSpc: undefined,
        unitOfMeasure: undefined,
        quantity: 1,
        itemCode: undefined,
        requestDate: undefined,
        sourceCode: undefined,
        productCode: undefined,
        productName: undefined,
        clientCode: undefined,
        clientName: undefined,
        workorderDesc: undefined,
        status: "草稿",
        parentId: undefined,
        remark: undefined,
      }
      this.resetForm("form")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.gettreeList()
    },
    // // 多选框选中数据
    // handleSelectionChange(selection) {
    //   this.ids = selection.map(item => item.routeId)
    //   this.single = selection.length!=1
    //   this.multiple = !selection.length
    // },
    /** 新增按钮操作 */
    handleAdd(row) {
      this.reset()
      this.dis = true
      if(row ){
        this.form.parentId = row.workorderId
      }else{
        this.form.parentId = 0
      }
      this.open = true
      this.title = "添加工艺流程"
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const workorderId = row.workorderId || this.ids
      getProcess(routeId).then(response => {
        this.form = response.data
        this.open = true
        this.title = "修改生产工单"
      })
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.workorderId != undefined) {
            updateProcess(this.form).then(response => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          } else {
            addWorkorder(this.form).then(response => {
              this.$modal.msgSuccess("新增成功")
              this.open = false
              this.gettreeList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const workorderId = row.workorderId
      this.$modal.confirm('是否确认删除岗位编号为"' + workorderId + '"的数据项？').then(function () {
        return delWorkorder(workorderId)
      }).then(res => {
        if (res.data === 0) {
          this.$modal.msgError("删除失败可能含有子项目")
        } else {
          this.gettreeList()
          this.$modal.msgSuccess("删除成功")
        }
      }).catch(() => {
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/workorder/export', {
        ...this.queryParams
      }, `workorder_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
